#include <string.h>
#include <stdio.h>


/* 7.18.1.1  Exact-width integer types */
typedef signed char int8_t;
typedef unsigned char   uint8_t;
typedef short  int16_t;
typedef unsigned short  uint16_t;
typedef int  int32_t;
typedef unsigned   uint32_t;
typedef long long  int64_t;
typedef unsigned long long   uint64_t;

#define Max_Length      200



//串口指令集
enum
{
    CMD_pic_show=0x70,  //显示图片  数据   pic_id
    CMD_pic_cut,       //图片区域复制  粘贴   pic_id  Xs,Ys,Xe,Ye,X,Y
};
//串口数据处理
typedef struct
{
    uint8_t buff[Max_Length];
    uint32_t Rx_Pointer;
    uint8_t page_id;

    uint16_t key_value;
    uint16_t xs;
    uint16_t ys;
    uint16_t xe;
    uint16_t ye;
    uint16_t x;
    uint16_t y;

} lcd_data_def;

lcd_data_def lcd_data;

void Lcd_CMD_Parser(void );

//
void lcd_data_ini(void)//初始化lcd接收buff
{

    lcd_data.Rx_Pointer=0;

    memset(lcd_data.buff,0,Max_Length);

}


void Lcd_Service(uint8_t dat)
{

    if ((lcd_data.buff[0] != 0xaa)||(lcd_data.Rx_Pointer>=Max_Length))//检查帧头是否是aa  数据缓存是否溢出
    {
        lcd_data_ini();
        lcd_data.buff[0]=dat;
        return;
    }

    lcd_data.buff[++lcd_data.Rx_Pointer]=dat;

    if( (lcd_data.Rx_Pointer>4)&&
            (lcd_data.buff[lcd_data.Rx_Pointer]==0x3c)&&
            (lcd_data.buff[lcd_data.Rx_Pointer-1]==0xc3)&&
            (lcd_data.buff[lcd_data.Rx_Pointer-2]==0x33)&&
            (lcd_data.buff[lcd_data.Rx_Pointer-3]==0xcc)
      )
    {
        Lcd_Cmd_Parser();
        lcd_data_ini();
    }

}

//lcd指令解析器
void Lcd_Cmd_Parser(void )
{
    switch(lcd_data.buff[1])
    {
    case  CMD_pic_show:
    {
        lcd_data.page_id=lcd_data.buff[2];
        // Show_Pic_2(lcd_data.page_id);
    }
    break;
    case  CMD_pic_cut:
    {
        lcd_data.page_id=lcd_data.buff[2];
        lcd_data.xs=(lcd_data.buff[3]<<8)|lcd_data.buff[4];
        lcd_data.ys=(lcd_data.buff[5]<<8)|lcd_data.buff[6];
        lcd_data.xe=(lcd_data.buff[7]<<8)|lcd_data.buff[8];
        lcd_data.ye=(lcd_data.buff[9]<<8)|lcd_data.buff[10];
        lcd_data.x=(lcd_data.buff[11]<<8)|lcd_data.buff[12];
        lcd_data.y=(lcd_data.buff[13]<<8)|lcd_data.buff[14];


        // cut_Pic_2(lcd_data.page_id,lcd_data.xs,lcd_data.ys,lcd_data.xe,lcd_data.ye,lcd_data.x,lcd_data.y);
    }
    break;
    default:
        break;

    }

}



uint8_t  LCD_BAG[10]= {0XAA,0X73,0X00,0X00,0X00,0X00,0XCC,0X33,0XC3,0X3C};
uint8_t  LCD_BAG1[8]= {0XAA,0X79,0X00,0X00,0XCC,0X33,0XC3,0X3C};

uint8_t HMIConfig_File[128];
//坐标-----键值解析器
//扫描当前页面的按钮，如果有按钮按下，则通过串口发出相应键值
void Lcd_Key_Parser(uint16_t x,uint16_t y)
{
    uint16_t i;

    uint8_t * data_pointer;
    uint8_t data_line[16];

    i=sizeof (HMIConfig_File)/sizeof (data_line);
    data_pointer=HMIConfig_File;

//	DrawRectangle(0,0,300,30,Red );
//	LCDPrintf(0,0,"X=%d  Y=%d  LCD_PAGE=%d",Touch_Bar.LCD_X,Touch_Bar.LCD_Y,Touch_Bar.LCD_PAGE);
    while(i)
    {
        memcpy(data_line,data_pointer,sizeof (data_line));
//	GLCDSPK(2);
        if(lcd_data.page_id==((data_line[0]<<8)|data_line[1]))
        {

            if(
                (x>=((data_line[2]<<8)|(data_line[3])	)	)&&
                (y>=((data_line[4]<<8)|(data_line[5])	)	)&&
                (x<=((data_line[6]<<8)|(data_line[7])	)	)&&
                (y<=((data_line[8]<<8)|(data_line[9])	)	)
            )
            {

                lcd_data.key_value =((data_line[14]<<8)|(data_line[15]));

                LCD_BAG1[2]=(uint8_t)(lcd_data.key_value>>8);
                LCD_BAG1[3]=(uint8_t)lcd_data.key_value;
                //HAL_UART_Transmit(&huart1,LCD_BAG1,sizeof(LCD_BAG1),10);

                return ;
            }
        }
        data_pointer+=sizeof (data_line);
        i--;
    }
}



int main(void)
{
    uint8_t cmdnuff[10]= {0xaa,0x70,0x05,0xcc,0x33,0xc3,0x3c};
    uint8_t i=0;

    while(i<7)
    {
        Lcd_Service(cmdnuff[i++]);


        if(i>=7)
        {

            i=0;
            cmdnuff[2]++;
            printf("page_id=%d\n",lcd_data.page_id);
        }

    }
    printf("hello\n");
    return 0;
}


